package com.ruoyi.train.service.impl;

import java.net.MalformedURLException;
import java.util.*;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itextpdf.io.image.ImageDataFactory;
import com.itextpdf.layout.element.Image;

import com.ruoyi.common.domain.TrainingInstitutionInfomation;
import com.ruoyi.common.service.ITrainingInstitutionInfomationService;

import com.ruoyi.config.pdf.method.PdfCreate;
import com.ruoyi.config.pdf.vo.PdfReqVo;

import com.ruoyi.train.domain.*;
import com.ruoyi.train.domain.Vo.ExamPassCertificateVo;
import com.ruoyi.train.domain.Vo.ExamPassStudentVo;
import com.ruoyi.train.service.*;
import com.ruoyi.common.core.domain.entity.SysDictData;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.service.ISysDictDataService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.train.mapper.ExamPassCertificateMapper;

/**
 * 考试合格证明Service业务层处理
 * 
 * @author wusihao
 * @date 2023-04-15
 */
@Service
public class ExamPassCertificateServiceImpl extends ServiceImpl<ExamPassCertificateMapper, ExamPassCertificate> implements IExamPassCertificateService
{
    @Autowired
    private ExamPassCertificateMapper examPassCertificateMapper;

    @Autowired
    private IExamPassStudentService examPassStudentService;

    @Autowired
    private IExamApplicationStudentService examApplicationStudentService;

    @Autowired
    private IExamApplicationService examApplicationService;
    @Autowired
    private IStudentInformationService studentInformationService;

    @Autowired
    private ITrainClassService trainClassService;

    @Autowired
    private IExamPassCertificateService examPassCertificateService;

    @Autowired
    private ICertApplicationService certApplicationService;

    @Autowired
    private ICertApplicationStudentService certApplicationStudentService;

    @Autowired
    private ITrainPlanService trainPlanService;

    @Autowired
    private IStudentProcessRecordService studentProcessRecordService;

    @Autowired
    private ITrainingInstitutionInfomationService trainingInstitutionInfomationService;

    @Autowired
    private ISysDictDataService dictDataService;

    @Override
    public ExamPassCertificateVo selectExamPassCertificateById(Long id) {
        ExamPassCertificate examPassCertificate = examPassCertificateMapper.selectExamPassCertificateById(id);
        ExamPassCertificateVo examPassCertificateVo = new ExamPassCertificateVo();
        // 将考试申请表信息加入Vo
        BeanUtils.copyProperties(examPassCertificate, examPassCertificateVo);
        // 通过考试合格证明表id查询考试合格学员表
        Long certificateId = examPassCertificate.getId();
        QueryWrapper<ExamPassStudent> examPassStudentQueryWrapper = new QueryWrapper<>();
        examPassStudentQueryWrapper.eq("exam_pass_id", certificateId);
        List<ExamPassStudent> examPassStudents = examPassStudentService.list(examPassStudentQueryWrapper);
        // 通过考试申请学生信息得到学生id
        Long studentId = examPassStudents.get(0).getStudentId();
        // 通过学生id得到考试申请id
        QueryWrapper<ExamApplicationStudent> examApplicationStudentQueryWrapper = new QueryWrapper<>();
        examApplicationStudentQueryWrapper.eq("student_id", studentId);
        List<ExamApplicationStudent> examApplicationStudents = examApplicationStudentService.list(examApplicationStudentQueryWrapper);
        ExamApplicationStudent studentServiceOne = examApplicationStudents.get(0);
        // 得到考试申请表id
        Long examApplicationId = studentServiceOne.getExamApplicationId();
        ExamApplication examApplication = examApplicationService.selectExamApplicationById(examApplicationId);
        examPassCertificateVo.setGradeSet(examApplication.getGradeSet());
        examPassCertificateVo.setExamplaceRecord(examApplication.getExamplaceRecord());
        examPassCertificateVo.setExamApplicationPdf(examApplication.getExamApplicationPdf());

        // 通过学生id查询班级id
        StudentInformation studentInformation = studentInformationService.getById(studentId);
        Long classId = studentInformation.getClassId();

        // 通过班级id查询培训班级信息
        TrainClass trainClass = trainClassService.getById(classId);

        examPassCertificateVo.setUrlClassTable(trainClass.getUrlClassTable());
        examPassCertificateVo.setUrlApplication(trainClass.getUrlApplication());
        examPassCertificateVo.setUrlSign(trainClass.getUrlSign());
        examPassCertificateVo.setUrlInvalue(trainClass.getUrlInvalue());
        examPassCertificateVo.setUrlCertificate(trainClass.getUrlCertificate());

        // 通过培训计划id查询培训计划表得到资格类型
        Long trainPlanId = trainClass.getTrainPlanId();
        TrainPlan trainPlanServiceById = trainPlanService.getById(trainPlanId);
        examPassCertificateVo.setQualifiedType(trainPlanServiceById.getQualifiedType());
        examPassCertificateVo.setWorkKind(trainPlanServiceById.getWorkKind());
        examPassCertificateVo.setTrainplanApplication(trainPlanServiceById.getTrainplanApplication());
        return examPassCertificateVo;
    }

    /**
     * 查询考试合格证明
     * 
     * @param id 考试合格证明主键
     * @return 考试合格证明
     */
    @Override
    public ExamPassCertificateVo selectExamPassCertificatePdfById(Long id)
    {
        ExamPassCertificate examPassCertificate = examPassCertificateMapper.selectExamPassCertificateById(id);
        ExamPassCertificateVo examPassCertificateVo = new ExamPassCertificateVo();
        // 将考试申请表信息加入Vo
        BeanUtils.copyProperties(examPassCertificate, examPassCertificateVo);

        // 用过审核人id得到审核人盖章
        Long auditId = examPassCertificate.getAuditId();
        QueryWrapper<TrainingInstitutionInfomation> trainingInstitutionInfomationQueryWrapper = new QueryWrapper<>();
        trainingInstitutionInfomationQueryWrapper.eq("create_id", auditId);
        TrainingInstitutionInfomation trainingInstitutionInfomationServiceOne = trainingInstitutionInfomationService.getOne(trainingInstitutionInfomationQueryWrapper);
        String applicationUnitSheet = trainingInstitutionInfomationServiceOne.getApplicationUnitSheet();
        examPassCertificateVo.setAuditSign(applicationUnitSheet);

        // 通过审批人id得到审批人盖章
        Long approvalId = examPassCertificate.getApprovalId();
        QueryWrapper<TrainingInstitutionInfomation> trainingInstitutionInfomationQueryWrapper1 = new QueryWrapper<>();
        trainingInstitutionInfomationQueryWrapper1.eq("create_id", approvalId);
        TrainingInstitutionInfomation trainingInstitutionInfomationServiceOne1 = trainingInstitutionInfomationService.getOne(trainingInstitutionInfomationQueryWrapper1);
        String applicationUnitSheet1 = trainingInstitutionInfomationServiceOne1.getApplicationUnitSheet();
        examPassCertificateVo.setApprovalSign(applicationUnitSheet1);

        // 通过盖章id得到盖章人信息
        Long examOperationSheet = examPassCertificate.getExamOperationSheet();
        QueryWrapper<TrainingInstitutionInfomation> trainingInstitutionInfomationQueryWrapper2 = new QueryWrapper<>();
        trainingInstitutionInfomationQueryWrapper2.eq("create_id", examOperationSheet);
        TrainingInstitutionInfomation trainingInstitutionInfomationServiceOne2 = trainingInstitutionInfomationService.getOne(trainingInstitutionInfomationQueryWrapper2);
        String applicationUnitSheet2 = trainingInstitutionInfomationServiceOne2.getApplicationUnitSheet();
        examPassCertificateVo.setCertificateChapter(applicationUnitSheet2);

        // 通过考试合格证明表id查询考试合格学员表
        Long certificateId = examPassCertificate.getId();
        QueryWrapper<ExamPassStudent> examPassStudentQueryWrapper = new QueryWrapper<>();
        examPassStudentQueryWrapper.eq("exam_pass_id", certificateId);
        List<ExamPassStudent> examPassStudents = examPassStudentService.list(examPassStudentQueryWrapper);
        // 通过考试申请学生信息得到学生id
        Long studentId = examPassStudents.get(0).getStudentId();
        // 通过学生id得到考试申请id
        QueryWrapper<ExamApplicationStudent> examApplicationStudentQueryWrapper = new QueryWrapper<>();
        examApplicationStudentQueryWrapper.eq("student_id", studentId);
        List<ExamApplicationStudent> examApplicationStudents = examApplicationStudentService.list(examApplicationStudentQueryWrapper);
        ExamApplicationStudent studentServiceOne = examApplicationStudents.get(0);
        // 得到考试申请表id
        Long examApplicationId = studentServiceOne.getExamApplicationId();
        ExamApplication examApplication = examApplicationService.selectExamApplicationById(examApplicationId);
        examPassCertificateVo.setGradeSet(examApplication.getGradeSet());
        examPassCertificateVo.setExamplaceRecord(examApplication.getExamplaceRecord());
        examPassCertificateVo.setExamApplicationPdf(examApplication.getExamApplicationPdf());

        // 通过学生id查询班级id
        StudentInformation studentInformation = studentInformationService.getById(studentId);
        Long classId = studentInformation.getClassId();

        // 通过班级id查询培训班级信息
        TrainClass trainClass = trainClassService.getById(classId);

        examPassCertificateVo.setUrlClassTable(trainClass.getUrlClassTable());
        examPassCertificateVo.setUrlApplication(trainClass.getUrlApplication());
        examPassCertificateVo.setUrlSign(trainClass.getUrlSign());
        examPassCertificateVo.setUrlInvalue(trainClass.getUrlInvalue());
        examPassCertificateVo.setUrlCertificate(trainClass.getUrlCertificate());

        // 通过培训计划id查询培训计划表得到资格类型
        Long trainPlanId = trainClass.getTrainPlanId();
        TrainPlan trainPlanServiceById = trainPlanService.getById(trainPlanId);
        examPassCertificateVo.setQualifiedType(trainPlanServiceById.getQualifiedType());
        examPassCertificateVo.setWorkKind(trainPlanServiceById.getWorkKind());
        examPassCertificateVo.setTrainplanApplication(trainPlanServiceById.getTrainplanApplication());
        return examPassCertificateVo;
    }

    /**
     * 培训机构查询考试合格证明列表
     * 
     * @param examPassCertificate 考试合格证明
     * @return 考试合格证明
     */
    @Override
    public List<ExamPassCertificateVo> selectExamPassCertificateList(ExamPassCertificate examPassCertificate)
    {
        // 返回的最终集合
        List<ExamPassCertificateVo> examPassCertificateVoList = new ArrayList<>();
        QueryWrapper<ExamPassCertificate> examPassCertificateQueryWrapper = new QueryWrapper<>();
        examPassCertificateQueryWrapper.eq("is_deleted", 0);
        examPassCertificateQueryWrapper.eq("is_pass", 0);
        examPassCertificateQueryWrapper.eq("create_id", SecurityUtils.getUserId());
        List<ExamPassCertificate> examPassCertificates = examPassCertificateService.list(examPassCertificateQueryWrapper);
        for (ExamPassCertificate passCertificate : examPassCertificates) {

            ExamPassCertificateVo examPassCertificateVo = new ExamPassCertificateVo();
            // 将考试申请表信息加入Vo
            BeanUtils.copyProperties(passCertificate, examPassCertificateVo);
            // 通过考试合格证明表id查询考试合格学员表
            Long certificateId = passCertificate.getId();
            QueryWrapper<ExamPassStudent> examPassStudentQueryWrapper = new QueryWrapper<>();
            examPassStudentQueryWrapper.eq("exam_pass_id", certificateId);
            List<ExamPassStudent> examPassStudents = examPassStudentService.list(examPassStudentQueryWrapper);
            if (examPassStudents.size() == 0) continue;
            // 通过考试申请学生信息得到学生id
            Long studentId = examPassStudents.get(0).getStudentId();
            // 通过学生id得到考试申请id
            QueryWrapper<ExamApplicationStudent> examApplicationStudentQueryWrapper = new QueryWrapper<>();
            examApplicationStudentQueryWrapper.eq("student_id", studentId);
            List<ExamApplicationStudent> examApplicationStudents = examApplicationStudentService.list(examApplicationStudentQueryWrapper);
            ExamApplicationStudent studentServiceOne = examApplicationStudents.get(0);
            // 得到考试申请表id
            Long examApplicationId = studentServiceOne.getExamApplicationId();
            ExamApplication examApplication = examApplicationService.selectExamApplicationById(examApplicationId);
            examPassCertificateVo.setGradeSet(examApplication.getGradeSet());
            examPassCertificateVo.setExamplaceRecord(examApplication.getExamplaceRecord());


            // 通过学生id查询班级id
            StudentInformation studentInformation = studentInformationService.getById(studentId);
            Long classId = studentInformation.getClassId();

            // 通过班级id查询培训班级信息
            TrainClass trainClass = trainClassService.getById(classId);

            examPassCertificateVo.setUrlClassTable(trainClass.getUrlClassTable());
            examPassCertificateVo.setUrlApplication(trainClass.getUrlApplication());
            examPassCertificateVo.setUrlSign(trainClass.getUrlSign());
            examPassCertificateVo.setUrlInvalue(trainClass.getUrlInvalue());
            examPassCertificateVo.setUrlCertificate(trainClass.getUrlCertificate());

            examPassCertificateVoList.add(examPassCertificateVo);
        }
        return examPassCertificateVoList;
    }

    // 经办人查询考试证明表
    @Override
    public List<ExamPassCertificateVo> jingbanrenselectExamPassCertificateList(ExamPassCertificate examPassCertificate) {
        // 返回的最终集合
        List<ExamPassCertificateVo> examPassCertificateVoList = new ArrayList<>();
        QueryWrapper<ExamPassCertificate> examPassCertificateQueryWrapper = new QueryWrapper<>();
        examPassCertificateQueryWrapper.eq("is_deleted", 0);
        examPassCertificateQueryWrapper.eq("approval_process_sign", 1);
        List<ExamPassCertificate> examPassCertificates = examPassCertificateService.list(examPassCertificateQueryWrapper);

        for (ExamPassCertificate passCertificate : examPassCertificates) {

            ExamPassCertificateVo examPassCertificateVo = new ExamPassCertificateVo();
            // 将考试申请表信息加入Vo
            BeanUtils.copyProperties(passCertificate, examPassCertificateVo);
            // 通过考试合格证明表id查询考试合格学员表
            Long certificateId = passCertificate.getId();
            QueryWrapper<ExamPassStudent> examPassStudentQueryWrapper = new QueryWrapper<>();
            examPassStudentQueryWrapper.eq("exam_pass_id", certificateId);
            List<ExamPassStudent> examPassStudents = examPassStudentService.list(examPassStudentQueryWrapper);
            // 通过考试申请学生信息得到学生id
            Long studentId = examPassStudents.get(0).getStudentId();
            // 通过学生id得到考试申请id
            QueryWrapper<ExamApplicationStudent> examApplicationStudentQueryWrapper = new QueryWrapper<>();
            examApplicationStudentQueryWrapper.eq("student_id", studentId);
            List<ExamApplicationStudent> examApplicationStudents = examApplicationStudentService.list(examApplicationStudentQueryWrapper);
            ExamApplicationStudent studentServiceOne = examApplicationStudents.get(0);
            // 得到考试申请表id
            Long examApplicationId = studentServiceOne.getExamApplicationId();
            ExamApplication examApplication = examApplicationService.selectExamApplicationById(examApplicationId);
            examPassCertificateVo.setGradeSet(examApplication.getGradeSet());
            examPassCertificateVo.setExamplaceRecord(examApplication.getExamplaceRecord());

            // 通过学生id查询班级id
            StudentInformation studentInformation = studentInformationService.getById(studentId);
            Long classId = studentInformation.getClassId();

            // 通过班级id查询培训班级信息
            TrainClass trainClass = trainClassService.getById(classId);

            examPassCertificateVo.setUrlClassTable(trainClass.getUrlClassTable());
            examPassCertificateVo.setUrlApplication(trainClass.getUrlApplication());
            examPassCertificateVo.setUrlSign(trainClass.getUrlSign());
            examPassCertificateVo.setUrlInvalue(trainClass.getUrlInvalue());
            examPassCertificateVo.setUrlCertificate(trainClass.getUrlCertificate());

            examPassCertificateVoList.add(examPassCertificateVo);
        }
        return examPassCertificateVoList;
    }

    // 负责人查询考试证明表
    @Override
    public List<ExamPassCertificateVo> fuzerenselectExamPassCertificateList(ExamPassCertificate examPassCertificate) {
        // 返回的最终集合
        List<ExamPassCertificateVo> examPassCertificateVoList = new ArrayList<>();
        QueryWrapper<ExamPassCertificate> examPassCertificateQueryWrapper = new QueryWrapper<>();
        examPassCertificateQueryWrapper.eq("is_deleted", 0);
        examPassCertificateQueryWrapper.eq("approval_process_sign", 2);
        List<ExamPassCertificate> examPassCertificates = examPassCertificateService.list(examPassCertificateQueryWrapper);

        for (ExamPassCertificate passCertificate : examPassCertificates) {

            ExamPassCertificateVo examPassCertificateVo = new ExamPassCertificateVo();
            // 将考试申请表信息加入Vo
            BeanUtils.copyProperties(passCertificate, examPassCertificateVo);
            // 通过考试合格证明表id查询考试合格学员表
            Long certificateId = passCertificate.getId();
            QueryWrapper<ExamPassStudent> examPassStudentQueryWrapper = new QueryWrapper<>();
            examPassStudentQueryWrapper.eq("exam_pass_id", certificateId);
            List<ExamPassStudent> examPassStudents = examPassStudentService.list(examPassStudentQueryWrapper);
            // 通过考试申请学生信息得到学生id
            Long studentId = examPassStudents.get(0).getStudentId();
            // 通过学生id得到考试申请id
            QueryWrapper<ExamApplicationStudent> examApplicationStudentQueryWrapper = new QueryWrapper<>();
            examApplicationStudentQueryWrapper.eq("student_id", studentId);
            List<ExamApplicationStudent> examApplicationStudents = examApplicationStudentService.list(examApplicationStudentQueryWrapper);
            ExamApplicationStudent studentServiceOne = examApplicationStudents.get(0);
            // 得到考试申请表id
            Long examApplicationId = studentServiceOne.getExamApplicationId();
            ExamApplication examApplication = examApplicationService.selectExamApplicationById(examApplicationId);
            examPassCertificateVo.setGradeSet(examApplication.getGradeSet());
            examPassCertificateVo.setExamplaceRecord(examApplication.getExamplaceRecord());

            // 通过学生id查询班级id
            StudentInformation studentInformation = studentInformationService.getById(studentId);
            Long classId = studentInformation.getClassId();

            // 通过班级id查询培训班级信息
            TrainClass trainClass = trainClassService.getById(classId);

            examPassCertificateVo.setUrlClassTable(trainClass.getUrlClassTable());
            examPassCertificateVo.setUrlApplication(trainClass.getUrlApplication());
            examPassCertificateVo.setUrlSign(trainClass.getUrlSign());
            examPassCertificateVo.setUrlInvalue(trainClass.getUrlInvalue());
            examPassCertificateVo.setUrlCertificate(trainClass.getUrlCertificate());

            examPassCertificateVoList.add(examPassCertificateVo);
        }
        return examPassCertificateVoList;
    }

    @Override
    public List<ExamPassCertificateVo> gaizhangExamPassCertificateList(ExamPassCertificate examPassCertificate) {
        // 返回的最终集合
        List<ExamPassCertificateVo> examPassCertificateVoList = new ArrayList<>();
        QueryWrapper<ExamPassCertificate> examPassCertificateQueryWrapper = new QueryWrapper<>();
        examPassCertificateQueryWrapper.eq("is_deleted", 0);
        examPassCertificateQueryWrapper.eq("approval_process_sign", 3);
        List<ExamPassCertificate> examPassCertificates = examPassCertificateService.list(examPassCertificateQueryWrapper);

        for (ExamPassCertificate passCertificate : examPassCertificates) {

            ExamPassCertificateVo examPassCertificateVo = new ExamPassCertificateVo();
            // 将考试申请表信息加入Vo
            BeanUtils.copyProperties(passCertificate, examPassCertificateVo);
            // 通过考试合格证明表id查询考试合格学员表
            Long certificateId = passCertificate.getId();
            QueryWrapper<ExamPassStudent> examPassStudentQueryWrapper = new QueryWrapper<>();
            examPassStudentQueryWrapper.eq("exam_pass_id", certificateId);
            List<ExamPassStudent> examPassStudents = examPassStudentService.list(examPassStudentQueryWrapper);
            // 通过考试申请学生信息得到学生id
            Long studentId = examPassStudents.get(0).getStudentId();
            // 通过学生id得到考试申请id
            QueryWrapper<ExamApplicationStudent> examApplicationStudentQueryWrapper = new QueryWrapper<>();
            examApplicationStudentQueryWrapper.eq("student_id", studentId);
            List<ExamApplicationStudent> examApplicationStudents = examApplicationStudentService.list(examApplicationStudentQueryWrapper);
            ExamApplicationStudent studentServiceOne = examApplicationStudents.get(0);
            // 得到考试申请表id
            Long examApplicationId = studentServiceOne.getExamApplicationId();
            ExamApplication examApplication = examApplicationService.selectExamApplicationById(examApplicationId);
            examPassCertificateVo.setGradeSet(examApplication.getGradeSet());
            examPassCertificateVo.setExamplaceRecord(examApplication.getExamplaceRecord());

            // 通过学生id查询班级id
            StudentInformation studentInformation = studentInformationService.getById(studentId);
            Long classId = studentInformation.getClassId();

            // 通过班级id查询培训班级信息
            TrainClass trainClass = trainClassService.getById(classId);

            examPassCertificateVo.setUrlClassTable(trainClass.getUrlClassTable());
            examPassCertificateVo.setUrlApplication(trainClass.getUrlApplication());
            examPassCertificateVo.setUrlSign(trainClass.getUrlSign());
            examPassCertificateVo.setUrlInvalue(trainClass.getUrlInvalue());
            examPassCertificateVo.setUrlCertificate(trainClass.getUrlCertificate());

            examPassCertificateVoList.add(examPassCertificateVo);
        }
        return examPassCertificateVoList;
    }

    /**
     * 新增考试合格证明
     * 
     * @param examPassCertificate 考试合格证明
     * @return 结果
     */
    @Override
    public int insertExamPassCertificate(ExamPassCertificate examPassCertificate)
    {
        examPassCertificate.setCreateTime(DateUtils.getNowDate());
        return examPassCertificateMapper.insertExamPassCertificate(examPassCertificate);
    }

    /**
     * 批量删除考试合格证明
     * 
     * @param ids 需要删除的考试合格证明主键
     * @return 结果
     */
    @Override
    public int deleteExamPassCertificateByIds(Long[] ids)
    {
        return examPassCertificateMapper.deleteExamPassCertificateByIds(ids);
    }

    /**
     * 删除考试合格证明信息
     * 
     * @param id 考试合格证明主键
     * @return 结果
     */
    @Override
    public int deleteExamPassCertificateById(Long id)
    {
        return examPassCertificateMapper.deleteExamPassCertificateById(id);
    }

    @Override
    public boolean submitExamPassCertificateTable(Long examPassCertificateId) {
        ExamPassCertificate examPassCertificate = examPassCertificateService.getById(examPassCertificateId);
        examPassCertificate.setApprovalProcessSign(1L);
        return examPassCertificateService.updateById(examPassCertificate);
    }

    // 经办人提交考试申请
    @Override
    public int jingbanrenApprovalSubmitCertificate(ExamPassCertificate examPassCertificate) {
        examPassCertificate.setAuditId(SecurityUtils.getUserId());
        if (examPassCertificate.getIsPass() == 1) {
            examPassCertificate.setApprovalProcessSign(2L);
            examPassCertificate.setIsPass(0);
        } else {
            examPassCertificate.setApprovalProcessSign(0L);
            examPassCertificate.setIsPass(-1);
        }

        return examPassCertificateService.updateById(examPassCertificate) ? 1 : 0;
    }

    // 负责人提交考试申请
    @Override
    public int fuzerenApprovalSubmitCertificate(ExamPassCertificate examPassCertificate) {
        examPassCertificate.setApprovalId(SecurityUtils.getUserId());
        if (examPassCertificate.getIsPass() == 1) {
            examPassCertificate.setApprovalProcessSign(3L);
            examPassCertificate.setIsPass(0);
        } else {
            examPassCertificate.setApprovalProcessSign(0L);
            examPassCertificate.setIsPass(-1);
        }
        return examPassCertificateService.updateById(examPassCertificate) ? 1 : 0;
    }

    @Override
    public boolean updateExamPassCertificateTable(ExamPassCertificate examPassCertificate) {

        return examPassCertificateService.updateById(examPassCertificate);
    }

    // 提交盖章1生成制证申请表
    @Override
    public int submitgaizhang1getCertificate(Long certificateId) {
        ExamPassCertificate examPassCertificate = examPassCertificateService.getById(certificateId);
        examPassCertificate.setExamOperationSheet(SecurityUtils.getUserId());
        examPassCertificate.setIsPass(1);
        examPassCertificateService.updateById(examPassCertificate);



        // 通过考试合格证明id得到考试合格证明表信息
        ExamPassCertificateVo examPassCertificateVo = selectExamPassCertificatePdfById(certificateId);
        // 通过考试合格证明表id得到考试合格证明表学生信息
        List<ExamPassStudentVo> examPassStudentVos = studentInformationService.selectStudentInformationByExamPassCertificateId(certificateId);
        examPassCertificateVo.setExamPassStudentVos(examPassStudentVos);

        // 通过考试合作证明表信息生成考试合格证明表pdf
        String pdfUrl = getExamCertificatePdfByExamCertificateInformation(examPassCertificateVo);
        ExamApplicationServiceImpl applicationService = new ExamApplicationServiceImpl();
        String url = applicationService.getPdfVirtualUrlByTrueUrl(pdfUrl);
        examPassCertificate.setExamCertificatePdf(url);
        // 存储上传的盖章图片
        examPassCertificate.setUpdateTime(DateUtils.getNowDate());
        examPassCertificate.setUpdateId(SecurityUtils.getUserId());
        examPassCertificate.setApprovalProcessSign(4L);
        examPassCertificate.setIsPass(1);
        int i = examPassCertificateMapper.updateExamPassCertificate(examPassCertificate);
        // 通过考试合格证明表id生成办证申请表信息
        int j = getCertInformationByExamCertificateId(certificateId);
        int success = 1;
        if (i == 0 || j == 0) success = 0;
        return success;
    }

    // TODO 上次服务器该地址
    // 通过考试合作证明表信息生成考试合格证明表pdf
    private String getExamCertificatePdfByExamCertificateInformation(ExamPassCertificateVo examPassCertificateVo) {
        // 得到资格类型
        String qualifiedType = examPassCertificateVo.getQualifiedType();
        Integer template = -1;
        if (qualifiedType.equals("105") || qualifiedType.equals("106")) template = 7;
        else template = 8;

        List<String> oneList = new ArrayList<>();
        if (examPassCertificateVo.getIdx() != null) oneList.add(examPassCertificateVo.getIdx());
        else oneList.add("");

        List<String> twoList = new ArrayList<>();
        if (examPassCertificateVo.getExamTerm() != null) twoList.add(examPassCertificateVo.getExamTerm());
        else twoList.add("");
        // 得到考试时间和补考时间
        String examTime = DateUtils.getFormatTime(examPassCertificateVo.getExamTime().toString()).substring(0, 10);
        String reviewTime = DateUtils.getFormatTime(examPassCertificateVo.getReviewTime().toString()).substring(0, 10);

        String time = examTime + "/" + reviewTime;
        if (time != null) twoList.add(time);
        else twoList.add("");

        int cnt = 0; // 合格人数
        List<List<String>> content = new ArrayList<>();
        List<ExamPassStudentVo> examPassStudentVos = examPassCertificateVo.getExamPassStudentVos();
        for (ExamPassStudentVo examPassStudentVo : examPassStudentVos) {
            List<String> studentInformation = new ArrayList<>();
            if (examPassStudentVo.getIdCard() != null) studentInformation.add(examPassStudentVo.getIdCard());
            else studentInformation.add("");
            if (examPassStudentVo.getName() != null) studentInformation.add(examPassStudentVo.getName());
            else studentInformation.add("");
            if (examPassStudentVo.getWorkplace() != null) studentInformation.add(examPassStudentVo.getWorkplace());
            else studentInformation.add("");
            if (examPassStudentVo != null) studentInformation.add(examPassStudentVo.getPhone());
            else studentInformation.add("");
            String qualifiedType1 = examPassStudentVo.getQualifiedType();
            if (qualifiedType1.equals("105") || qualifiedType1.equals("106")) {
                SysDictData dictData = dictDataService.selectDictDataById(Long.parseLong(qualifiedType1));
                String dictLabel = dictData.getDictLabel();
                String workKind = examPassStudentVo.getWorkKind();
                if (workKind != null) {
                    SysDictData dictData1 = dictDataService.selectDictDataById(Long.parseLong(workKind));
                    String dictLabel1 = dictData1.getDictLabel();
                    studentInformation.add(dictLabel + dictLabel1);
                } else studentInformation.add("");

            } else if (qualifiedType1.equals("107")) {
                String operationType = examPassStudentVo.getOperationType();
                if (operationType != null) {
                    SysDictData dictData = dictDataService.selectDictDataById(Long.parseLong(operationType));
                    String dictLabel = dictData.getDictLabel();
                    studentInformation.add(dictLabel);
                } else studentInformation.add("");

            } else studentInformation.add("");
            if (examPassStudentVo.getExamScore() != null) studentInformation.add(examPassStudentVo.getExamScore());
            else studentInformation.add("");
            if (qualifiedType.equals("107")) {
                if (examPassStudentVo.getOperatingScore() != null)
                    studentInformation.add(examPassStudentVo.getOperatingScore());
                else studentInformation.add("");
            }
            if (examPassStudentVo.getExamConclusion() != null) {
                if (examPassStudentVo.getExamConclusion().equals("合格")) {
                    cnt ++ ;
                }
                studentInformation.add(examPassStudentVo.getExamConclusion());
            }

            else studentInformation.add("");
            content.add(studentInformation);
        }
        List<String> approvalList = new ArrayList<>();
        approvalList.add("");
        approvalList.add("");
        String submitTime = DateUtils.getFormatTime(examPassCertificateVo.getSubmitTime().toString()).substring(0, 10);
        approvalList.add("九江市安全生产考试机构\n" + submitTime.substring(0, 4) + "年" + submitTime.substring(5, 7) + "月" + submitTime.substring(8, 10) + "日");

        Map<String, Image> signMap = new HashMap<>();
        Map<String, Image> sealMap = new HashMap<>();
        try {
            String auditSign = examPassCertificateVo.getAuditSign();
            if (auditSign != null) {
                auditSign = "D:/ruoyi/uploadPath" + auditSign.substring(8);
//                auditSign = "C:/wusihao/uploadPath" + auditSign.substring(8);
                Image auditSignImage = new Image(ImageDataFactory.create(auditSign));
                signMap.put("经办人", auditSignImage);
            }

            String approvalSign = examPassCertificateVo.getApprovalSign();
            if (approvalSign != null) {
                approvalSign = "D:/ruoyi/uploadPath" + approvalSign.substring(8);
//                approvalSign = "C:/wusihao/uploadPath" + approvalSign.substring(8);
                Image approvalSignImage = new Image(ImageDataFactory.create(approvalSign));
                signMap.put("负责人：", approvalSignImage);
            }

            String certificateChapter = examPassCertificateVo.getCertificateChapter();
            if (approvalSign != null) {
               certificateChapter = "D:/ruoyi/uploadPath" + certificateChapter.substring(8);
//                certificateChapter = "C:/wusihao/uploadPath" + certificateChapter.substring(8);
                Image certificateChapterImage = new Image(ImageDataFactory.create(certificateChapter));
                sealMap.put("考试机构：", certificateChapterImage);
            }
        } catch (MalformedURLException e) {
            throw new RuntimeException(e);

        }

        List<String> finalDataList = new ArrayList<>();
        finalDataList.add("" + cnt);

        // 生成pdf
        PdfReqVo pdfReqVo = new PdfReqVo();
        pdfReqVo.setTemplate(template);
        pdfReqVo.setOneBasicList(oneList);
        pdfReqVo.setTwoBasicList(twoList);
        pdfReqVo.setContent(content);
        pdfReqVo.setFinalDataList(finalDataList);
        pdfReqVo.setApprovalList(approvalList);
        pdfReqVo.setSealMap(sealMap);
        pdfReqVo.setSignMap(signMap);
        try {
            String pdf = PdfCreate.createPdf(pdfReqVo);
            return pdf;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }


    // 通过考试合格证明表信息生成办证申请表信息
    private int getCertInformationByExamCertificateId(Long certificateId) {
        // 存储答案
        CertApplication certApplication = new CertApplication();
        // 通过合格证明表id找到学生集合
        QueryWrapper<ExamPassStudent> examPassStudentQueryWrapper = new QueryWrapper<>();
        examPassStudentQueryWrapper.eq("exam_pass_id", certificateId);
        List<ExamPassStudent> examPassStudents = examPassStudentService.list(examPassStudentQueryWrapper);
        certApplication.setSum((long) examPassStudents.size());
        certApplication.setCreateId(certApplication.getCreateId());
        certApplicationService.save(certApplication);
        Long certApplicationId = certApplication.getId();

        for (ExamPassStudent examPassStudent : examPassStudents) {
            Long studentId = examPassStudent.getStudentId();
            CertApplicationStudent certApplicationStudent = new CertApplicationStudent();
            certApplicationStudent.setStudentId(studentId);
            certApplicationStudent.setCertApplicationId(certApplicationId);
            certApplicationStudentService.save(certApplicationStudent);
        }

        // 通过学生id找到学生信息表
        Long studentId = examPassStudents.get(0).getStudentId();
        QueryWrapper<StudentInformation> studentInformationQueryWrapper = new QueryWrapper<>();
        studentInformationQueryWrapper.eq("id", studentId);
        StudentInformation studentInformationServiceById = studentInformationService.getOne(studentInformationQueryWrapper);
        // 通过学生信息的到班级id
        Long classId = studentInformationServiceById.getClassId();
        // 通过班级id的到培训计划id
        TrainClass trainClassServiceById = trainClassService.getById(classId);
        Long trainPlanId = trainClassServiceById.getTrainPlanId();
        // 通过培训计划id找到资格类型
        TrainPlan trainPlanServiceById = trainPlanService.getById(trainPlanId);
        String qualifiedType = trainPlanServiceById.getQualifiedType();
        CertApplication certApplicationServiceById = certApplicationService.getById(certApplicationId);
        certApplicationServiceById.setQualifiedType(qualifiedType);
        certApplicationServiceById.setCreateId(trainPlanServiceById.getCreateId());
        return certApplicationService.updateCertApplication(certApplicationServiceById);
    }
}
